﻿using D.Application.Contracts.Dtos;

namespace D.Application.Command.Commands
{
    /// <summary>
    /// 添加命令
    /// </summary>
    /// <param name="id"></param>
    public sealed record SysOrgUpdateCommand(SysOrgDto sysOrgDto) : ICommand;
    internal sealed class SysOrgUpdateandler : ICommandHandler<SysOrgUpdateCommand>
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly ISysOrgRepository _sysOrgRepository;
        private readonly SysOrgDomainService _sysOrgDomainService;

        public SysOrgUpdateandler(ISysOrgRepository sysOrgRepository, SysOrgDomainService sysOrgDomainService, IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _sysOrgDomainService = sysOrgDomainService;
            _sysOrgRepository = sysOrgRepository;
        }

        public async Task<Result> Handle(SysOrgUpdateCommand command, CancellationToken cancellationToken)
        {
            SysOrgEntity? sysOrg = await _sysOrgRepository.GetByIdAsync(command.sysOrgDto.Id);
            if (sysOrg is null)
            {
                return SysOrgErrors.NotFound(command.sysOrgDto.Id);
            }

            // 调用domain的函数
            _sysOrgDomainService.AddAsync();

            // 执行事务提交
            _unitOfWork.SaveChangesAsync(cancellationToken);

            return new Result();
        }
    }
}
